/*
 * ﻿Copyright (C) 2011-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.aspectjutil.dynamicagent;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.newmainsoftech.aspectjutil.dynamicagent.DynamicAspectJWeaverAgent;

/**
 * This class is just code holder, in order to take advantage of IDE's content assist and 
 * other features for coding beanshell script being used in ant file of this project.
 * The code here better be either ones demanded before Maven's test phase (so no need to say why not being 
 * able to move to source folder) or ones not suitable to put into any of test classes. Otherwise, such 
 * code should be moved to suitable test class and beanshell script in ant file should refer the yielded 
 * test class in the test output directory. <br />  
 * Coding note: Beanshell does not handle Generic and Annotation.
 * 
 * @author <a href="mailto:artymt@gmail.com">Arata Y.</a>
 */
public class ForBeanshell {
	// For beanshell script at declar_DynamicAspectJWeaverAgent_name_property_target Ant target ---
	public static final String MavenProjectReferenceId = "maven.project";
		public static String getMavenprojectreferenceid() {
			return MavenProjectReferenceId;
		}
	/**
	 * Set name of {@link DynamicAspectJWeaverAgent} class to Maven property. 
	 * Called from beanshell script at declar_DynamicAspectJWeaverAgent_name_property_target Ant target. 
	 * 
	 * @param propertyName
	 * @param antProject
	 * @throws Throwable
	 */
	public void setMavenPropertyForDynamicAspectJWeaverAgentName( 
			final String propertyName, final Object antProject) 
	throws Throwable {
		Method getReferenceMethod = 
		antProject.getClass().getMethod( "getReference", new Class<?>[]{ String.class});
			getReferenceMethod.setAccessible( true);
		Object mavenProject 
		= getReferenceMethod.invoke( antProject, ForBeanshell.getMavenprojectreferenceid());
		
		Method getPropertiesMethod 
		= mavenProject.getClass().getMethod( "getProperties", (Class<?>[])null);
			getPropertiesMethod.setAccessible( true);
		Properties properties 
		= (Properties)(getPropertiesMethod.invoke(mavenProject, (Object[])null));
		properties.setProperty( propertyName, DynamicAspectJWeaverAgent.class.getName());
	}
	// --------------------------------------------------------------------------------------------
}
